//
//  CTMessageView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/23.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTMessageView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTAppLanguage.h"
#import "CTDVSwitch.h"

#import "CTPersonalMessageView.h"
#import "CTOfficialMessageView.h"
#import "CTMessageLogic.h"
#import "CTDataGenerator.h"
#import "CTLogin.h"
#import "CTAppInitFile.h"
#import "CTTabBarController.h"

@interface CTMessageView ()<CTPersonalMessageViewDelegate,CTOfficialMessageViewDelegate,CTMessageLogicDelegate>
{
    UIView * topView;
    
    UILabel * personalMesgLabel;
    UILabel * operationMesgLabel;
    
    CTPersonalMessageView * personalMesgView;
    CTOfficialMessageView * officialMesgView;
    
    UIImageView * mesgPersonalMarkImageView;
    UILabel * mesgPersonalRemaindLabel;
    UIImageView * mesgOfficialMarkImageView;
    UILabel * mesgOfficialRemaindLabel;
    
    CTMessageLogic * mesgLogic;
    
    UIButton * clearBtn;
}

@end

@implementation CTMessageView
@synthesize delegate;

- (void)dealloc
{
    [clearBtn removeFromSuperview];
    clearBtn = nil;
    
    mesgLogic = nil;
    mesgLogic.delegate = nil;
    
    [mesgPersonalMarkImageView removeFromSuperview];
    mesgPersonalMarkImageView = nil;
    [mesgPersonalRemaindLabel removeFromSuperview];
    mesgPersonalRemaindLabel = nil;
    [mesgOfficialMarkImageView removeFromSuperview];
    mesgOfficialMarkImageView = nil;
    [mesgOfficialRemaindLabel removeFromSuperview];
    mesgOfficialRemaindLabel = nil;
    
    [topView removeFromSuperview];
    topView = nil;
    
    [personalMesgLabel removeFromSuperview];
    personalMesgLabel = nil;
    
    [operationMesgLabel removeFromSuperview];
    operationMesgLabel = nil;
    
    [personalMesgView removeFromSuperview];
    personalMesgView = nil;
    
    [officialMesgView removeFromSuperview];
    officialMesgView = nil;
    
    delegate = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CTPushNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CTLoginSuccessNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CTExitSuccessNotification object:nil];
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTMessageView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        
        mesgLogic = [[CTMessageLogic alloc] init];
        mesgLogic.delegate = self;
        
        //推送
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotiMesg:) name:CTPushNotification object:nil];
        
        //注册登录&退出
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:CTLoginSuccessNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitSuccess:) name:CTExitSuccessNotification object:nil];
        
        [self creatSubviews];
    }
    return self;
}

-(void)creatSubviews
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    UIView * topLineView = [[UIView alloc] initWithFrame:CGRectMake(0,63.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topLineView.backgroundColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
    [topView addSubview:topLineView];
    
    UIImageView * separateLineImageView = [[UIImageView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH - 0.5)/2.0, 33.5, 0.5, 17)];
    separateLineImageView.image = [UIImage imageNamed:@"biaotilanfengexian"];
    [topView addSubview:separateLineImageView];
    
    
    
    clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
     clearBtn.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH - 5 - 40, 34.5, 40, 15);
    [clearBtn setTitle:[CTAppLanguage localizedStringForKey:@"messageView_clearBtn_clear" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    clearBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [clearBtn addTarget:self action:@selector(clearBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [clearBtn setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    clearBtn.hidden = YES;
    [topView addSubview:clearBtn];
    
    
    
    
    
    
    
    UIButton * personalMesgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    personalMesgBtn.tag = 100;
    personalMesgBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH - 0.5)/2.0-100, 20, 100, 44);
    [personalMesgBtn addTarget:self action:@selector(selectMessageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:personalMesgBtn];
    
    UIButton * operationMesgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    operationMesgBtn.tag = 101;
    operationMesgBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH + 0.5)/2.0, 20, 100, 44);
    [operationMesgBtn addTarget:self action:@selector(selectMessageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:operationMesgBtn];
    
    personalMesgLabel = [[UILabel alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH - 0.5)/2.0 - 30 -50, 34, 50, 15)];
    personalMesgLabel.textColor = CT_HEADER_GREEN_COLOR;
    personalMesgLabel.textAlignment = NSTextAlignmentRight;
    personalMesgLabel.text = [CTAppLanguage localizedStringForKey:@"messageView_personalMesgLabel" withValue:nil withTable:@"Language"];
    [topView addSubview:personalMesgLabel];
    
    operationMesgLabel = [[UILabel alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH + 0.5)/2.0 + 30, 34, 50, 15)];
    operationMesgLabel.textColor = CT_HEADER_DEEPLINE_COLOR;
    operationMesgLabel.text = [CTAppLanguage localizedStringForKey:@"messageView_operationMesgLabel" withValue:nil withTable:@"Language"];
    [topView addSubview:operationMesgLabel];
    
    
    
    
    
    personalMesgView = [[CTPersonalMessageView alloc] init];
    personalMesgView.hideView = NO;
    personalMesgView.delegate = self;
    [self addSubview:personalMesgView];
    
    officialMesgView = [[CTOfficialMessageView alloc] init];
    officialMesgView.hideView = YES;
    officialMesgView.delegate = self;
    [self addSubview:officialMesgView];
    
    
    
    //消息
    mesgPersonalMarkImageView = [[UIImageView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH - 0.5)/2.0 - 34,29, 23, 15)];
    mesgPersonalMarkImageView.image= [UIImage imageNamed:@"weiduxiaoxi_bg"];
    [topView addSubview:mesgPersonalMarkImageView];
    mesgPersonalMarkImageView.hidden = YES;
    mesgPersonalRemaindLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,2.5, 23, 10)];
    mesgPersonalRemaindLabel.font = [UIFont systemFontOfSize:10.0];
    mesgPersonalRemaindLabel.textAlignment = NSTextAlignmentCenter;
    mesgPersonalRemaindLabel.textColor = CT_HEADER_WHITE_COLOR;
    [mesgPersonalMarkImageView addSubview:mesgPersonalRemaindLabel];
    
    
    mesgOfficialMarkImageView = [[UIImageView alloc] initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH + 0.5)/2.0+30+30,29, 23, 15)];
    mesgOfficialMarkImageView.image= [UIImage imageNamed:@"weiduxiaoxi_bg"];
    [topView addSubview:mesgOfficialMarkImageView];
    mesgOfficialMarkImageView.hidden = YES;
    mesgOfficialRemaindLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,2.5, 23, 10)];
    mesgOfficialRemaindLabel.font = [UIFont systemFontOfSize:10.0];
    mesgOfficialRemaindLabel.textAlignment = NSTextAlignmentCenter;
    mesgOfficialRemaindLabel.textColor = CT_HEADER_WHITE_COLOR;
    [mesgOfficialMarkImageView addSubview:mesgOfficialRemaindLabel];
    

    
    
//    NSString * segmentCellOneStr = [CTAppLanguage localizedStringForKey:@"messageView_segment_one" withValue:nil withTable:@"Language"];
//    NSString * segmentCellTwoStr = [CTAppLanguage localizedStringForKey:@"messageView_segment_two" withValue:nil withTable:@"Language"];
//    CTDVSwitch *switchView = [CTDVSwitch switchWithStringsArray:@[segmentCellOneStr,segmentCellTwoStr]];
//    switchView.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH - 145)/2.0,28,145,30);
//    switchView.backgroundColor = [UIColor clearColor];
//    switchView.sliderColor = [UIColor clearColor];
//    switchView.labelTextColorInsideSlider = CT_HEADER_GREEN_COLOR;
//    switchView.labelTextColorOutsideSlider = CT_HEADER_DEEPLINE_COLOR;
//    switchView.cornerRadius = 14;
//    switchView.layer.borderWidth = 1;
//    switchView.layer.borderColor = [UIColor clearColor].CGColor;
//    switchView.layer.cornerRadius = 14;
//    [topView addSubview:switchView];
//    
//    [switchView setPressedHandler:^(NSUInteger index) {
//        switch (index) {
//            case 0:
//                //消息
//               
//                break;
//            case 1:
//                //通知
//                
//                break;
//            default:
//                break;
//        }
//        
//    }];
}





#pragma mark - action&delegate -

-(void)clearBtnAction:(id)sender
{
    if (personalMesgView.hidden == YES) {
        if ([self.delegate respondsToSelector:@selector(clearAction:withType:)]) {
            [self.delegate clearAction:self withType:CTMessageViewTypeOfficial];
        }
    }else{
        if ([self.delegate respondsToSelector:@selector(clearAction:withType:)]) {
            [self.delegate clearAction:self withType:CTMessageViewTypePersonal];
        }
    }
    
}

-(void)selectMessageViewBtnAction:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    switch (btn.tag) {
        case 100:
            //个人消息
            personalMesgLabel.textColor = CT_HEADER_GREEN_COLOR;
            operationMesgLabel.textColor = CT_HEADER_DEEPLINE_COLOR;
            
            personalMesgView.hideView = NO;
            officialMesgView.hideView = YES;
            
            clearBtn.hidden = YES;
            
            [mesgLogic deleteTemporaryMessageData:CTMessageLogicTemporaryTypePersonal];
            
            
            break;
        case 101:
            //运营消息
            personalMesgLabel.textColor = CT_HEADER_DEEPLINE_COLOR;
            operationMesgLabel.textColor = CT_HEADER_GREEN_COLOR;
            
            personalMesgView.hideView = YES;
            officialMesgView.hideView = NO;
            
            clearBtn.hidden = NO;
            
            [mesgLogic deleteTemporaryMessageData:CTMessageLogicTemporaryTypeOfficical];
            
            break;
        default:
            break;
    }
    
    CTTabBarController * tabbarVC = [CTAppInitFile rootTabbar];
    [tabbarVC notiCount];
}



-(void)refresh
{
    [personalMesgView reloadData];
}
-(void)refreshPullDownEnd
{
    [personalMesgView refreshPullDownEnd];
}
-(void)refreshPullUpEnd
{
    [personalMesgView refreshPullUpEnd];
}

-(void)reloadData
{
    [personalMesgView reloadData];
}

-(void)viewWillShow
{
    if (personalMesgView.hidden == NO) {
        [mesgLogic deleteTemporaryMessageData:CTMessageLogicTemporaryTypePersonal];
    }else{
        [mesgLogic deleteTemporaryMessageData:CTMessageLogicTemporaryTypeOfficical];
    }
    
    CTTabBarController * tabbarVC = [CTAppInitFile rootTabbar];
    [tabbarVC notiCount];
}

#pragma mark - setter -

-(void)setPersonalMessageArr:(NSArray *)personalMessageListArr
{
    personalMesgView.listArr = personalMessageListArr;
}

-(void)setOfficialMessageArr:(NSArray *)officialMessageListArr
{
    officialMesgView.listArr = officialMessageListArr;
}


#pragma mark - personal message delegate -

-(void)selectPersonalMessageAction:(CTMessageListItem *)itm
{
    if ([self.delegate respondsToSelector:@selector(selectPersonalMessageItemAction:withItem:)]) {
        [self.delegate selectPersonalMessageItemAction:self withItem:itm];
    }
}

-(void)deletePersonalMessageAction:(CTMessageListItem *)itm
{
    if ([self.delegate respondsToSelector:@selector(removePersonalMessageItemAction:withItem:)]) {
        [self.delegate removePersonalMessageItemAction:self withItem:itm];
    }
}


-(void)refreshControlPullDown:(CTPersonalMessageView *)view
{
    if ([self.delegate respondsToSelector:@selector(refreshPersonalMesgControlPullDown:)]) {
        [self.delegate refreshPersonalMesgControlPullDown:self];
    }
}
-(void)refreshControlPullUp:(CTPersonalMessageView *)view
{
    if ([self.delegate respondsToSelector:@selector(refreshPersonalMesgControlPullUp:)]) {
        [self.delegate refreshPersonalMesgControlPullUp:self];
    }
}

-(void)personMesgImageClicked:(CTPersonalMessageView *)view withItem:(CTMessageListItem *)itm
{
    if ([self.delegate respondsToSelector:@selector(personalImageClicked:withItem:)]) {
        [self.delegate personalImageClicked:self withItem:itm];
    }
}


#pragma mark - official message delegate -

-(void)selectOfficialMessgaeAction:(CTOfficialMessageItem *)itm
{
    if ([self.delegate respondsToSelector:@selector(selectOfficialMessageItemAction:withItem:)]) {
        [self.delegate selectOfficialMessageItemAction:self withItem:itm];
    }
}

-(void)deleteOfficialMessageAction:(CTOfficialMessageItem *)itm
{
    if ([self.delegate respondsToSelector:@selector(removeOfficialMessageItemAction:withItem:)]) {
        [self.delegate removeOfficialMessageItemAction:self withItem:itm];
    }
}




#pragma mark - mesg logic delegate -

-(void)pushNotiMesg:(id)sender
{
    NSInteger personal_count = [mesgLogic mesgTemporaryDataCount:CTMessageLogicTemporaryTypePersonal];
    NSInteger official_count = [mesgLogic mesgTemporaryDataCount:CTMessageLogicTemporaryTypeOfficical];
    
    if (personal_count == 0) {
        mesgPersonalMarkImageView.hidden = YES;
        mesgPersonalRemaindLabel.text = @"0";
    }else{
        mesgPersonalMarkImageView.hidden = NO;
        mesgPersonalRemaindLabel.text = [NSString stringWithFormat:@"%ld",(long)personal_count];
    }
    
    if (official_count == 0) {
        mesgOfficialMarkImageView.hidden = YES;
        mesgOfficialRemaindLabel.text = @"0";
    }else{
        mesgOfficialMarkImageView.hidden = NO;
        mesgOfficialRemaindLabel.text = [NSString stringWithFormat:@"%ld",(long)official_count];
    }
    
    personalMesgView.listArr = [CTDataGenerator generatePersonalMessageListItems];
    officialMesgView.listArr = [CTDataGenerator generateOfficialMessageListItems];
}

-(void)mesgTemporaryAdded
{
    
}
-(void)mesgTemporaryRemoved
{
    NSInteger personal_count = [mesgLogic mesgTemporaryDataCount:CTMessageLogicTemporaryTypePersonal];
    NSInteger official_count = [mesgLogic mesgTemporaryDataCount:CTMessageLogicTemporaryTypeOfficical];
    
    if (personal_count == 0) {
        mesgPersonalMarkImageView.hidden = YES;
        mesgPersonalRemaindLabel.text = @"0";
    }else{
        mesgPersonalMarkImageView.hidden = NO;
        mesgPersonalRemaindLabel.text = [NSString stringWithFormat:@"%ld",(long)personal_count];
    }
    
    if (official_count == 0) {
        mesgOfficialMarkImageView.hidden = YES;
        mesgOfficialRemaindLabel.text = @"0";
    }else{
        mesgOfficialMarkImageView.hidden = NO;
        mesgOfficialRemaindLabel.text = [NSString stringWithFormat:@"%ld",(long)official_count];
    }
    
}



#pragma mark - login & exit -

-(void)loginSuccess:(id)sender
{
    
}
-(void)exitSuccess:(id)sender
{
   
}

@end
